﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using iTextSharp.text.pdf;
using iTextSharp.text;

/*
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;
using MS.WindowsAPICodePack.Internal;
*/


namespace pdfmerger
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            /*
            JumpList.CreateJumpList();
            JumpListLink notepad = new JumpListLink(@"C:\Windows\notepad.exe", "Launch Notepad");
            
           
            JumpListItem doc1 = new JumpListItem(@"doc1.txt");
            JumpListItem doc2 = new JumpListItem(@"doc2.txt");

            JumpListCustomCategory category = new JumpListCustomCategory("Special Items");
            category.AddJumpListItems(doc1);
            category.AddJumpListItems(doc2);

            JumpList.AddUserTasks();
            JumpList.CreateJumpList();
            //JumpList.AddCustomCategories(category);
              */

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                String outputfile = saveFileDialog1.FileName.ToString();

                
                String filename1 = openFileDialog1.FileName.ToString();
                PdfReader reader1 = new PdfReader(filename1);
                int pagenumber1 = reader1.NumberOfPages;

                
                
                
                String filename2 = openFileDialog2.FileName.ToString();
                PdfReader reader2 = new PdfReader(filename2);
                int pagenumber2 = reader2.NumberOfPages;


                Document outputdoc = new Document(reader1.GetPageSizeWithRotation(1));
                PdfCopy writer = new PdfCopy(outputdoc, new FileStream(outputfile, FileMode.Create));

                outputdoc.Open();

                PdfImportedPage page;
                PdfImportedPage page2;


                if ((pagenumber1 - pagenumber2) == 0 || (pagenumber1 - pagenumber2) == 1) 
                {


                    for (int i = 1; i <= pagenumber1; i++)
                    {
                        page = writer.GetImportedPage(reader1, i);
                        writer.AddPage(page);


                        if (i <= pagenumber2)  //Wenn Doc 1 eine Seite mehr hat wird beim letzten Schleifendurchlauf dieser Abschnitt übersprungen
                        {
                            if (absteigend_button.Checked)
                            {
                                page2 = writer.GetImportedPage(reader2, pagenumber1 + 1 - i);
                                
                                writer.AddPage(page2);
                            }

                            if (aufsteigend_button.Checked)
                            {
                                page2 = writer.GetImportedPage(reader2, i);
                                writer.AddPage(page2);
                            }
                        }

                    }

                    PRAcroForm form = reader1.AcroForm;
                    writer.CopyAcroForm(reader1);

                    outputdoc.Close();

                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = outputfile;
                    
                    p.Start();

                }
                else
                {
                    MessageBox.Show("Das 1. Dokument darf maximal eine Seite mehr haben als das zweite Dokument");
                }





            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }




        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void file2_button_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            zielpfad_lable.Text = saveFileDialog1.FileName.ToString();
        }
    }
}
